GameLoop
Merhabalar, kötü de olsa bir loop yapıp ekranda kayan bir imaj oluşturacaktım, fakat olmadı, neden olmadı?
[code]
package com.example.simplegame;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
GameScreen gs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gs=new GameScreen(this);
setContentView(gs);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package com.example.simplegame;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
@SuppressLint("WrongCall")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class GameScreen extends View{
Bitmap harun;
int x=20, y=20;
GameLoop gl;
public GameScreen(Context c) {
super(c);
harun = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
gl = new GameLoop(this);
gl.setRunning(true);
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
canvas.drawBitmap(harun, x, y, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
x = (int) event.getX() - harun.getWidth()/2;
y = (int) event.getY() - harun.getHeight()/2;
return super.onTouchEvent(event);
}
}
package com.example.simplegame;
import android.annotation.SuppressLint;
import android.graphics.Canvas;
public class GameLoop extends Thread{
GameScreen gv;
private boolean running = false;
public GameLoop(GameScreen gv) {
super();
this.gv = gv;
}
@SuppressLint("WrongCall")
@Override
public void run() {
super.run();
while(running){
Canvas c = null;
if(gv.y < gv.getHeight()-100)
{
gv.y += 10;
}
gv.onDraw(c);
try{
Thread.sleep(10);
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public void setRunning(boolean run){
running = run;
}
}
[/code]